package com.kgc.xsProvider.service.ServiceImpl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kgc.basic.util.PageInfo;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.xs.pojo.*;
import com.kgc.xsProvider.dao.SalesOutMapper;
import com.kgc.xsProvider.service.SalesOutService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class SalesOutServiceImpl implements SalesOutService {

    private Logger logger = LoggerFactory.getLogger(SalesOutServiceImpl.class);

    @Autowired
    private SalesOutMapper salesOutMapper;


    @Override
    public ResponseMsg selectSalesOutByPage(Map<String, Object> map) {
        logger.info(" SalesOutServiceImpl selectSalesOutByPage params{}", map.toString());
        ResponseMsg responseMsg = new ResponseMsg();
        PageInfo<SalesOut> pageInfo = new PageInfo<SalesOut>();
        pageInfo.setPageNo(map.get("pageNo")==null?1:Integer.parseInt(map.get("pageNo").toString()));
        pageInfo.setPageSize(map.get("pageSize")==null?10:Integer.parseInt(map.get("pageSize").toString()));
        Page page= PageHelper.startPage(pageInfo.getPageNo(),pageInfo.getPageSize());
        List<SalesOut> list=salesOutMapper.selectSalesOutByPage(map);
        pageInfo.setList(list);
        pageInfo.setTotal((int)page.getTotal());
        responseMsg.setData(pageInfo);
        logger.debug("SalesOutServiceImpl selectSalesOutByPage return {}",responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg insertSalesOut(Map<String, Object> map) {
        logger.info("SalesOutServiceImpl selectSalesOutByPage params{}",map.toString());
        ResponseMsg responseMsg=new ResponseMsg();
        ObjectMapper mapper = new ObjectMapper();

        // 转换  对象
        SalesOut salesOut = mapper.convertValue(
                map.get("salesOut"),
                SalesOut.class
        );
        List<?> rawDetails = (List<?>) map.get("details");

// 使用 ObjectMapper 转换集合类型
        List<SalesOutDetail> details = mapper.convertValue(
                rawDetails,
                new TypeReference<List<SalesOutDetail>>() {}
        );
        int result1=salesOutMapper.insertSalesOut(salesOut);
        try {
            for (SalesOutDetail detail : details) {
                salesOutMapper.insertSalesOutDetail(detail);
            }
            responseMsg.setCode("200");
        }catch (Exception e){
            responseMsg.setCode("201");

            return responseMsg;
        }
        if(result1>0){
            responseMsg.setCode("200");
        }else {
            responseMsg.setCode("201");
        }
        logger.info("SalesOutServiceImpl selectSalesOutByPage responseMsg{}",responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg updateSalesOut(Map<String, Object> map) {
        return null;
    }

    @Override
    public ResponseMsg deleteSalesOut(String code) {
        logger.info("SalesOutServiceImpl deleteSalesOut code {}",code);
        ResponseMsg responseMsg=new ResponseMsg();
        int result1=salesOutMapper.deleteSalesOut(code);
        int result2=salesOutMapper.deleteSalesOutDetail(code);
        if(result1>0&&result2>0){
            responseMsg.setCode("200");
        }else {
            responseMsg.setCode("201");
        }
        logger.info("SalesOutServiceImpl deleteSalesOut return {}",responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg selectSalesOutDetail(String code) {
        logger.info("SalesOutServiceImpl selectSalesOutDetail code {}",code);
        ResponseMsg responseMsg=new ResponseMsg();
        List<SalesOutDetail> list=salesOutMapper.selectSalesOutDetailByPage(code);
        responseMsg.setData(list);
        logger.debug("SalesOutServiceImpl selectSalesOutDetail return {}",responseMsg);
        return responseMsg;
    }
}
